﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FierceWar
{
	public class BuildMapPopupMenuScreen : Screen
	{
		private BuildMapScreen _buildMapScreen;

		private MenuEntriesManager _menuEntriesManager;
		private MenuEntry _save;
		private MenuEntry _saveAs;
		private MenuEntry _help;
		private MenuEntry _resume;
		private MenuEntry _quit;

		private Rectangle _rectangle;

		public BuildMapPopupMenuScreen(GameResources resources,
			List<EventHandler> screenEvents, BuildMapScreen buildMapScreen)
			: base(resources, screenEvents)
		{
			_buildMapScreen = buildMapScreen;

			_save = new MenuEntry(resources, "Save", 0.0f, 0.0f);
			_saveAs = new MenuEntry(resources, "Save As", 0.0f, 0.0f);
			_help = new MenuEntry(resources, "Help", 0.0f, 0.0f);
			_resume = new MenuEntry(resources, "Resume", 0.0f, 0.0f);
			_quit = new MenuEntry(resources, "Quit", 0.0f, 0.0f);

			_menuEntriesManager = new MenuEntriesManager(resources, screenEvents);
			_menuEntriesManager.MenuEntries.Add(_save);
			_menuEntriesManager.MenuEntries.Add(_saveAs);
			_menuEntriesManager.MenuEntries.Add(_help);
			_menuEntriesManager.MenuEntries.Add(_resume);
			_menuEntriesManager.MenuEntries.Add(_quit);
			_menuEntriesManager.OriginY = 150.0f;
			_menuEntriesManager.DistanceInterval = 60.0f;
			_menuEntriesManager.InitializePositions();

			foreach (MenuEntry mEntry in _menuEntriesManager.MenuEntries)
			{
				mEntry.NormalColor = Color.WhiteSmoke * 0.15f;
				mEntry.ShadowOffset = Vector2.One;
			}

			int width = 250;
			int height = 320;
			_rectangle = new Microsoft.Xna.Framework.Rectangle(
					(Resources.GraphicsDevice.Viewport.Width - width) / 2,
					(Resources.GraphicsDevice.Viewport.Height - height) / 2,
					width, height);
		}

		public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
		{
			base.Update(gameTime);

			_menuEntriesManager.Update(gameTime);

			if (KeyboardInput.IsKeyPressed(
				Microsoft.Xna.Framework.Input.Keys.Escape))
			{
				ScreenEvents[ScreenEvents.Count - 1].
					Invoke(this, new EventArgs());
			}
		}

		public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
		{
			base.Draw(gameTime);

			if (_buildMapScreen != null)
			{
				_buildMapScreen.Draw(gameTime);
			}
			FadeToBlack(0.6f);

			Resources.SpriteBatch.Draw(Resources.Board3, _rectangle, Color.White);
			_menuEntriesManager.Draw();
		}
	}
}
